<?php
class Admin_UserController extends My_Controllers_Admin {
	public function indexAction() {
		$form = new My_Forms_Admin_User ( );
		if ($this->_request->isPost ()) {
			$formData = $this->_request->getPost ();
			if ($form->isValid ( $formData )) {
				Zend_Debug::dump ( $formData, formData, true );
				if ($formData ['passWord'] == $formData ['passWord_re']) {
					$auth = Zend_Auth::getInstance ();
					$id = $auth->getIdentity ()->id;
					$user = new User ( );
					$userDb = $user->find ( $id );
					$passWordDb = $userDb->current ()->password;
					Zend_Debug::dump ( $passWordDb, "passWordDb", false );
					
					if ($passWordDb == md5 ( $formData ['passWord_old'] )) {
						$userDb->current ()->password = md5 ( $formData ['passWord'] );
						$userDb->current ()->save ();
						//						Zend_Debug::dump ( $userDb->current ()->password );
						$this->_redirect ( "/admin/login/logout" );
					} else {
						$this->_redirect ( "/admin/user" );
					}
				} else {
					$this->_redirect ( "/admin/user" );
					//					$this->_redirect ( "message", "index", null, array ("message" => "ok" ) );
				}
			} else {
				$form->populate ( $formData );
			}
		}
		$this->view->form = $form;
		Zend_Debug::dump ( $form->__toString (), "form", false );
	}
	public function listAction() {
		$db = Zend_Registry::get ( 'db' );
		$select = $db->select ();
		$select->from ( array ('u' => 'bc_users' ), array ('id', 'username', 'group', 'email' ) );
		$select->joinLeft ( array ('k' => 'bc_keshi' ), 'u.ke_id = k.id', array ('kename' => 'kename', 'kid' => 'id' ) );
		$sql = $select->__toString ();
		//		Zend_Debug::dump ( $sql );
		//		Zend_Debug::dump ( $db->fetchAll ( $sql ) );
		$arr = new My_Helper_Array ( );
		$this->view->userArray = $arr->array_group_by ( $db->fetchAll ( $sql ), 'kid' );
		//		Zend_Debug::dump ( $this->view->userArray );
	}
	public function msgAction() {
		if ($this->_getParam ( 'id' )) {
			$action = 'updata';
		} else {
			$action = 'insert';
		}
		$kid = $this->_getParam ( 'kid' );
		$username = $this->_getParam ( 'username' );
		$email = $this->_getParam ( 'email' );
		$url = $this->view->url ( array ('action' => $action ) );
		$form = new My_Forms_Admin_Msg ( $kid, $url, $action, $username, $email );
		$this->view->form = $form;
	}
	public function insertAction() {
		$front = Zend_Controller_Front::getInstance ();
		$front->setParam ( 'noViewRenderer', true );
		
		$form = new My_Forms_Admin_Msg ( );
		$formData = $this->_request->getPost ();
		if ($form->isValid ( $formData )) {
			array_pop ( $formData );
			Zend_Debug::dump ( $formData );
			$user = new User ( );
			
			try {
				$user->insert ( $formData );
				$this->_redirect ( '/admin/user/list' );
			} catch ( PDOException $e ) {
				Zend_Debug::dump ( $e->getMessage () );
			}
		} else {
			$form->populate ( $formData );
		}
	}
	public function updataAction() {
		$front = Zend_Controller_Front::getInstance ();
		$front->setParam ( 'noViewRenderer', true );
		
		$form = new My_Forms_Admin_Msg ( null, null, 'updata' );
		$formData = $this->_request->getPost ();
		
		if ($form->isValid ( $formData )) {
			array_pop ( $formData );
			$formData ['group'] = 'member';
			Zend_Debug::dump ( $formData );
			$user = new User ( );
			
			try {
				$where = $user->getAdapter ()->quoteInto ( 'id = ?', $this->_getParam ( 'id' ) );
				$user->update ( $formData, $where );
				$this->_redirect ( '/admin/' );
			} catch ( PDOException $e ) {
				Zend_Debug::dump ( $e->getMessage () );
			}
		} else {
			echo $form->populate ( $formData );
		}
	}
}
